#!/usr/bin/env python 
import MySQLdb
from class_segment import *
import tool_chunk

def get_all_lg(mysql_cursor) :
  req = 'SELECT DISTINCT `lg` FROM `Document`;'
  mysql_cursor.execute(req)

base_db = 'align_ecr_selected'

conn = MySQLdb.connect('localhost','root','ephrael_faith', base_db)
curs = conn.cursor() 

get_all_lg(curs)

corpus_multidoc = {}
id_chunk = 1;

doc2bloc = {}
id_bloc2text = {}
bloc2chunk = {}

curs = [['fr'],['es']]

for lg in curs :
  curs_rel = conn.cursor()
  corres = tool_chunk.lg2relative_id_doc(lg[0],curs_rel)
  for ti,i in corres.iteritems() :
    if corpus_multidoc.has_key(i) == False :
      corpus_multidoc[i] = {}
    corpus_multidoc[i][lg[0]] = ti


for id_multidoc,multidoc in corpus_multidoc.iteritems() :
  for lg,id_doc in multidoc.iteritems() :    
    curs_a = conn.cursor()
    tool_chunk.reader_id_document2array(id_doc,curs_a)
    for info_bloc in curs_a :
      id_bloc = info_bloc[0]
      id_bloc2text[id_bloc] = info_bloc[1]
      curs_w = conn.cursor()
      array_word = tool_chunk.reader_id_bloc2info_chunk(id_bloc,curs_w)
      if (bloc2chunk.has_key(lg) == False) :
        bloc2chunk[lg] = {}
      if (bloc2chunk[lg].has_key(id_doc) == False) :
        bloc2chunk[lg][id_doc] = {}
      bloc2chunk[lg][id_doc][id_bloc] = array_word

id_multidoc = 1
multidoc = corpus_multidoc[id_multidoc]
lg_src = 'fr'
lg_tgt = 'es'
id_doc_src = multidoc[lg_src]
id_doc_tgt = multidoc[lg_tgt]
curs_corres = conn.cursor()
array_corres_src = tool_chunk.lg2relative_id_bloc(lg_src,curs_corres) 
array_corres_tgt = tool_chunk.lg2relative_id_bloc(lg_tgt,curs_corres)
#print multidoc
#print id_doc_src,id_doc_tgt
print array_corres_src

bc_src = bloc2chunk[lg_src][id_doc_src]
bc_tgt = bloc2chunk[lg_tgt][id_doc_tgt]
cpt = 0


list_id_bloc_src = bc_src.keys()
list_id_bloc_src.sort()
list_id_bloc_tgt = bc_tgt.keys()
list_id_bloc_tgt.sort()

assert(len(list_id_bloc_tgt) == len(list_id_bloc_src))

for k in range(0,len(list_id_bloc_tgt)) :
  k1 = list_id_bloc_src[k]
  k2 = list_id_bloc_tgt[k]
  v1 = bc_src[k1]
  v2 = bc_tgt[k2]
  ac_src = tool_chunk.bc2ac(v1,id_bloc2text[k1])
  ac_tgt = tool_chunk.bc2ac(v2,id_bloc2text[k2])

  curs_rstr = conn.cursor()
#  rstr_src = tool_chunk.chunk2rstr_info(ac_src,k1,curs_rstr)
  rstr_src = tool_chunk.chunk2rstr(ac_src,k1,curs_rstr)
  rstr_tgt = tool_chunk.chunk2rstr(ac_tgt,k2,curs_rstr)

  for chunk_info_src in rstr_src.iteritems() :
    id_src = chunk_info_src[0]
    print "[",ac_src[id_src],"]"
    print ">>>>>"
    for info_src in chunk_info_src[1] :
      curs_rep = conn.cursor()
      rep_src = tool_chunk.lg_graphie2repartition_bloc(lg_src,info_src[3],array_corres_src,curs_rep)
      print "++++",info_src[3],rep_src

#    for chunk_info_tgt in rstr_tgt.iteritems() :
#      id_tgt = chunk_info_tgt[0]
#      print "[",ac_tgt[id_tgt],"]"
#      print "<<<<<"
#      for info_tgt in chunk_info_tgt[1] :
#        curs_rep = conn.cursor()
#        rep_tgt = tool_chunk.lg_graphie2repartition_bloc(lg_tgt,info_tgt[3],array_corres_tgt,curs_rep)
#        print "   ",info_tgt[3],rep_tgt


  1/0

#      for chunk_info in rstr_src.iteritems() :
#        for info in chunk_info[1] :



  for chunk_src in ac_src :
    for chunk_tgt in ac_tgt :
      print chunk_src, "::" ,chunk_tgt
      print type(chunk_src)

  1/0


1/0
  
for k1,v1 in bc_src.iteritems() :
#  print k1
  ac_src = tool_chunk.bc2ac(v1,id_bloc2text[k1])
  if(cpt == 1) :
    for c in ac_src :
      print id_bloc2text[k1]
      print c, c.get_offset(), c.get_len()
      print "===="

  curs_rstr = conn.cursor()
  rstr_src = tool_chunk.chunk2rstr(ac_src,k1,curs_rstr)
  print rstr_src
  for i,rstr_info in rstr_src.iteritems() :
    print rstr_info
  1/0

  if(cpt == 1 ) :
    1/0
  cpt += 1

#id_doc_tgt = corpus_multidoc[lg_tgt]
#print bloc2chunk

#  $r = $array_obj_sql['sql_rstr']->reader_all2relative_id_doc($lg["lg"]);

#foreach($data_lg as $lg){
#  $r = $array_obj_sql['sql_rstr']->reader_all2relative_id_doc($lg["lg"]);
#  $k = key($r);
#  foreach($r[$k] as $tid=>$id){
#    $corpus_multidoc[$id][$k] = $tid;
#  }
#}

#result=curs.fetchone() 
#print result; 
#result=curs.fetchall() 
#print result;
#curs.close() 
#conn.close()
